package com.lq.thread;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

public class CakeAddMoney {

    static AtomicReference<Integer> money = new AtomicReference<>();

    static AtomicStampedReference<Integer> money2 = new AtomicStampedReference<>(19, 0);

    static {
        money.set(19);
    }

    public static void main(String[] args) {
//        reCharge();
//        pay();

        reCharge2();
        pay2();
    }

    private static void reCharge() {
        for (int i = 0; i < 3; i++) {
            new Thread() {
                public void run () {
                    while (true) {
                        while (true) {
                            Integer m = money.get();
                            if (m < 20) {
                                if (money.compareAndSet(m, m + 20)) {
                                    System.out.println("金额小于20，充值成功，余额：" + money.get());
                                    break;
                                }
                            } else {
                                break;
                            }
                        }
                    }
                }
            }.start();
        }
    }

    private static void pay () {
        new Thread() {
            public void run() {
                for (int i = 0; i < 10; i++) {
                    while (true) {
                        Integer m = money.get();
                        if (m > 10) {
                            System.out.println("金额大于10元");
                            if (money.compareAndSet(m, m - 10)) {
                                System.out.println("成功消费10元， 余额：" + money.get());
                                break;
                            }
                        } else {
                            System.out.println("金额不足");
                            break;
                        }
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {

                    }
                }
            }
        }.start();
    }

    private static void reCharge2() {
        for (int i = 0; i < 3; i++) {
            final int timestamp = money2.getStamp();
            new Thread() {
                public void run () {
                    while (true) {
                        while (true) {
                            Integer m = money2.getReference();
                            if (m < 20) {
                                if (money2.compareAndSet(m, m + 20, timestamp, timestamp + 1)) {
                                    System.out.println("金额小于20，充值成功，余额：" + money.get());
                                    break;
                                }
                            } else {
                                break;
                            }
                        }
                    }
                }
            }.start();
        }
    }

    private static void pay2 () {
        new Thread() {
            public void run() {
                for (int i = 0; i < 5; i++) {
                    while (true) {
                        int timestamp = money2.getStamp();
                        Integer m = money2.getReference();
                        if (m > 10) {
                            System.out.println("金额大于10元");
                            if (money2.compareAndSet(m, m - 10, timestamp, timestamp + 1)) {
                                System.out.println("成功消费10元， 余额：" + money2.getReference());
                                break;
                            }
                        } else {
                            System.out.println("金额不足");
                            break;
                        }
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {

                    }
                }
            }
        }.start();
    }
}
